<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>更改权限</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/font-awesome.css" rel="stylesheet">
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/default/layer.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <link href="css/layui.css" rel="stylesheet">
<style>

    layui-input-block—me{
        .margin-left: 80px;
        min-height: 36px;
    }
</style>
</head>
<body class="gray-bg">
    <div class="wrapper wrapper-content">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-content">
                        <div class="form-horizontal">
                            <div class="form-group">
                                <form class="form-horizontal m-t" id="commentForm">
                                    <div class="col-md-12  layui-form">

                                        <div class="layui-form-item">
                                            <!--layui-form-label-->
                                            <label class="col-xs-3 control-label" style="margin-left: -8px">应用：</label>
                                            <div class="layui-input-block—me">
                                                <input type="radio" name="sex" value="0" title="app" checked="" lay-filter="app">
                                                <input type="radio" name="sex" value="1" title="pc网页" lay-filter="app">
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label class="col-xs-3 control-label">角色：</label>
                                            <div class="layui-input-inline">
                                                <select name="role" id="role">
                                                    <option value="0" selected="">普通用户</option>
                                                    <option value="1" >管理员</option>
                                                </select>
                                            </div>
                                        </div>

                                    </div>
                                </form>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <div class="col-xs-3 col-xs-offset-3">
                                    <button class="btn btn-success" type="submit">确定</button>
                                </div>
                                <div class="col-xs-3">
                                    <button class="btn btn-danger" type="submit">取消</button>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- 全局js -->
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <!-- 自定义js -->
    <script src="js/content.js"></script>
    <script src="js/plugins/layer/layer.min.js"></script>
    <script src="js/plugins/layer/layer.js"></script>
    <script src="js/layui.js"></script>
    <script src="js/plugins/validate/jquery.validate.min.js"></script>
    <script src="js/plugins/validate/messages_zh.min.js"></script>
    <script src="js/work/common.js"></script>
    <script>
        var selectUserId = "";
        // 默认为android应用appKey
        var selectAppKey = androidAppKey;
        var selectRoleId = "";
        var form;
        layui.use(['form'], function(){
            form = layui.form;

            form.on('radio(app)', function (data) {
                if (data.value == 0) {          // 选择的是app
                    selectAppKey = androidAppKey;
                    initRoleInfo(androidAppKey);
                } else if (data.value == 1) {   // 选择的是pc
                    selectAppKey = appKey;
                    initRoleInfo(appKey);
                }
            });

        });
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        $(document).ready(function () {
            selectUserId = getStorage("userId");
            initRoleInfo(androidAppKey);

            // 取消按钮
            $(".btn-danger").on("click", function () {
                parent.layer.close(index);
            });

            var flag=0;
            $(".btn-success").on("click", function () {
                selectRoleId = $("#role").val();

                ajaxPost({
                    url: "role/updateUserRole",
                    data: {
                        userId:selectUserId,
                        appKey:selectAppKey,
                        roleId:selectRoleId
                    },
                    successx: function (data) {
                        setTimeout(function(){
                            flag=0;
                        },200);
                        if (data.success) {
                            parent.layer.msg(data.message);
                            setTimeout(function () {
                                parent.window.location.href = "usermanage.html";
                                parent.layer.close(index);
                            }, 2000)

                        } else {
                            parent.layer.msg(data.message)
                        }
                    }
                });
            });
        });

        function initRoleInfo(appkey) {
            ajaxPost({
                url: "role/queryRoleByAppId",
                data: {
                    appKey:appkey,
                },
                successx: function (data) {
                    if (data.success) {
                        createHtml(data.data);
                    } else {
                        layer.msg(data.message)
                    }
                }
            });
        }

        function createHtml(data) {
            $("#role").html("");
            if (data != null) {
                var optionHtml = "";
                for (var i = 0; i < data.length ; i++) {
                    var option = '<option value="'+data[i].roleId+'">'+data[i].roleNameZh+'</option>';
                    optionHtml += option;
                }
                $("#role").html(optionHtml);
                form.render();
            }
        }

    </script>
</body>
</html>
